//
//  UIView+XMGExtension.m
//  5期-百思不得姐
//
//  Created by xiaomage on 15/11/6.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

#import "UIView+XMGExtension.h"

@implementation UIView (XMGExtension)

- (CGSize)xmg_size
{
    return self.frame.size;
}

- (void)setXmg_size:(CGSize)xmg_size
{
    CGRect frame = self.frame;
    frame.size = xmg_size;
    self.frame = frame;
}

- (CGFloat)xmg_width
{
    return self.frame.size.width;
}

- (CGFloat)xmg_height
{
    return self.frame.size.height;
}

- (void)setXmg_width:(CGFloat)xmg_width
{
    CGRect frame = self.frame;
    frame.size.width = xmg_width;
    self.frame = frame;
}

- (void)setXmg_height:(CGFloat)xmg_height
{
    CGRect frame = self.frame;
    frame.size.height = xmg_height;
    self.frame = frame;
}

- (CGFloat)xmg_x
{
    return self.frame.origin.x;
}

- (void)setXmg_x:(CGFloat)xmg_x
{
    CGRect frame = self.frame;
    frame.origin.x = xmg_x;
    self.frame = frame;
}

- (CGFloat)xmg_y
{
    return self.frame.origin.y;
}

- (void)setXmg_y:(CGFloat)xmg_y
{
    CGRect frame = self.frame;
    frame.origin.y = xmg_y;
    self.frame = frame;
}

- (CGFloat)xmg_centerX
{
    return self.center.x;
}

- (void)setXmg_centerX:(CGFloat)xmg_centerX
{
    CGPoint center = self.center;
    center.x = xmg_centerX;
    self.center = center;
}

- (CGFloat)xmg_centerY
{
    return self.center.y;
}

- (void)setXmg_centerY:(CGFloat)xmg_centerY
{
    CGPoint center = self.center;
    center.y = xmg_centerY;
    self.center = center;
}

- (CGFloat)xmg_right
{
//    return self.xmg_x + self.xmg_width;
    return CGRectGetMaxX(self.frame);
}

- (CGFloat)xmg_bottom
{
//    return self.xmg_y + self.xmg_height;
    return CGRectGetMaxY(self.frame);
}

- (void)setXmg_right:(CGFloat)xmg_right
{
    self.xmg_x = xmg_right - self.xmg_width;
}

- (void)setXmg_bottom:(CGFloat)xmg_bottom
{
    self.xmg_y = xmg_bottom - self.xmg_height;
}

+ (instancetype)viewFromXib
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

- (BOOL)intersectWithView:(UIView *)view
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect selfRect = [self convertRect:self.bounds toView:window];
    CGRect viewRect = [view convertRect:view.bounds toView:window];
    return CGRectIntersectsRect(selfRect, viewRect);
}


- (UIView*)subViewOfClassName:(NSString*)className {
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}

-(void)showEaseInOutAnimation{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5];
    self.transform=CGAffineTransformMakeScale(1,1);
    [UIView commitAnimations];
}


-(void)hideEaseInOutAnimation{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5];
    self.transform=CGAffineTransformMakeScale(0.0001, 0.0001);
    [UIView commitAnimations];
}

-(void)LoadAnimationLayerColor:(UIColor *)layerColor animationLayerCount:(NSInteger)layerCount{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.backgroundColor = layerColor.CGColor;
    shapeLayer.bounds = CGRectMake(0, 0, 10, 10);
    shapeLayer.position = CGPointMake(self.bounds.size.width/2, 20);
    shapeLayer.borderColor = [UIColor whiteColor].CGColor;
    shapeLayer.cornerRadius = 5;
    shapeLayer.borderWidth = 0.5;
    shapeLayer.transform = CATransform3DMakeScale(.0, .0, .0);
    
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    ani.duration = 1;
    ani.repeatCount = HUGE;//无限循环
    ani.fromValue = @1;
    ani.toValue = @0;
    [shapeLayer addAnimation:ani forKey:nil];
    
    //CAReplicatorLayer用于对图层进行复制，包括图层的动画也能复制！可以看着将某一段事务进行重复！
    CAReplicatorLayer *repLayer = [CAReplicatorLayer layer];
    repLayer.frame = self.bounds;
    
    [repLayer addSublayer:shapeLayer];
    repLayer.instanceCount = layerCount; //复制数量
    repLayer.instanceDelay = .1;//颜色
    //沿着z轴旋转360度
    repLayer.instanceTransform = CATransform3DMakeRotation(M_PI*2/layerCount, 0, 0, 1);
    [self.layer addSublayer:repLayer];
}
@end
